package com.muqi.app.qlearn.adapter;

import java.util.List;

import com.easemob.easeui.widget.CircularImage;
import com.muqi.app.network.http.GlideImageUtils;
import com.muqi.app.qlearn.modles.CommentBean;
import com.muqi.app.qlearn.student.R;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

public class CommentAdapter extends BaseAdapter {
	private Context context;
	private List<CommentBean> listData;
	private boolean isHome=false;
	
	public void setHome(boolean isHome) {
		this.isHome = isHome;
	}

	public CommentAdapter(Context context, List<CommentBean> listData) {
		this.context = context;
		this.listData = listData;
	}

	@Override
	public int getCount() {
		return listData == null ? 10 : listData.size();
	}

	@Override
	public CommentBean getItem(int position) {
		return listData.get(position);
	}
   
	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressLint("InflateParams") @Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		final CommentBean itemInfo = listData.get(position);
		if (convertView == null) {
			holder = new ViewHolder();
			if(isHome){
				convertView = LayoutInflater.from(context).inflate(R.layout.item_comment_home, null);
			}else{
				convertView = LayoutInflater.from(context).inflate(R.layout.item_comment, null);
			}
		
			holder.itemAvatar = (CircularImage) convertView.findViewById(R.id.comment_circularImage);
			holder.itemName = (TextView) convertView.findViewById(R.id.comment_name);
			holder.itemReply = (TextView) convertView.findViewById(R.id.comment_reply);
			holder.itemRyName = (TextView) convertView.findViewById(R.id.comment_reply_name);
			holder.itemTime = (TextView) convertView.findViewById(R.id.comment_time);
			holder.itemContent = (TextView) convertView.findViewById(R.id.comment_content);
			holder.itemRoot = (LinearLayout) convertView
					.findViewById(R.id.rl_root);
			holder.itemLine=convertView.findViewById(R.id.view_line);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		GlideImageUtils.setImageLoader(context, itemInfo.author_userInfo.avatarInfo.fileurl_01, 
					holder.itemAvatar, R.drawable.default_avator);
		holder.itemName.setText(itemInfo.author_userInfo.name+"");
		holder.itemTime.setText(itemInfo.create_time+"");
		holder.itemContent.setText(itemInfo.content+"");
		if(itemInfo.replay_userInfo!=null){
			holder.itemReply.setVisibility(View.VISIBLE);
			holder.itemRyName.setVisibility(View.VISIBLE);
			holder.itemRyName.setText(itemInfo.replay_userInfo.name+"");
		}else{
			holder.itemReply.setVisibility(View.INVISIBLE);
			holder.itemRyName.setVisibility(View.INVISIBLE);
		}
		
		holder.itemLine.setVisibility(View.VISIBLE);
		if(!isHome){
			if(listData.size()==1){
				holder.itemRoot
				.setBackgroundResource(R.drawable.corners_gray_5_radus);
				holder.itemLine.setVisibility(View.GONE);
			}else{
				if (position == 0) {
					holder.itemRoot
							.setBackgroundResource(R.drawable.corners_gray_half_top);
				} else if (position == listData.size() - 1) {
					holder.itemLine.setVisibility(View.GONE);
					holder.itemRoot
							.setBackgroundResource(R.drawable.corners_gray_half_bottom);
				} else {
					holder.itemRoot.setBackgroundColor(context.getResources().getColor(
							R.color.record_detial_bg));
				}
			}
		}
		
		
		return convertView;
	}
	
	private class ViewHolder {
		CircularImage itemAvatar;
		TextView itemName;
		TextView itemReply;
		TextView itemRyName;
		TextView itemTime;
		TextView itemContent;
		LinearLayout itemRoot;
		View itemLine;
	}
}
